namespace LicenseApp.Client;

/// <summary>
/// 授权通知接口
/// </summary>
public interface ILicenseNotifier
{
    /// <summary>
    /// 显示授权状态通知
    /// </summary>
    /// <param name="message">消息内容</param>
    /// <param name="status">授权状态</param>
    void ShowNotification(string message, LicenseStatus status);
    
    /// <summary>
    /// 显示即将到期提醒
    /// </summary>
    /// <param name="remainingDays">剩余天数</param>
    void ShowExpiringNotification(int remainingDays);
    
    /// <summary>
    /// 显示已过期通知
    /// </summary>
    void ShowExpiredNotification();
    
    /// <summary>
    /// 显示状态变化通知
    /// </summary>
    /// <param name="isValid">是否有效</param>
    void ShowStatusChangeNotification(bool isValid);
}

/// <summary>
/// 自定义窗口通知实现
/// </summary>
public class CustomWindowNotifier : ILicenseNotifier
{
    public void ShowNotification(string message, LicenseStatus status)
    {
        NotificationWindow.ShowNotification(message, status);
    }
    
    public void ShowExpiringNotification(int remainingDays)
    {
        string message = remainingDays > 0 
            ? $"授权将在{remainingDays}天后到期，请及时续期" 
            : "授权今日到期，请立即续期";
        
        ShowNotification(message, LicenseStatus.ExpiringSoon);
    }
    
    public void ShowExpiredNotification()
    {
        ShowNotification("授权已过期，请联系供应商续期", LicenseStatus.Expired);
    }
    
    public void ShowStatusChangeNotification(bool isValid)
    {
        string message = isValid ? "授权状态已恢复正常" : "授权状态异常，请检查";
        LicenseStatus status = isValid ? LicenseStatus.Valid : LicenseStatus.Expired;
        
        ShowNotification(message, status);
    }
}

/// <summary>
/// 控制台通知实现（用于控制台应用）
/// </summary>
public class ConsoleNotifier : ILicenseNotifier
{
    public void ShowNotification(string message, LicenseStatus status)
    {
        string prefix = status switch
        {
            LicenseStatus.Valid => "[信息]",
            LicenseStatus.ExpiringSoon => "[警告]",
            LicenseStatus.ExpiredGracePeriod => "[提醒]",
            LicenseStatus.Expired => "[错误]",
            _ => "[通知]"
        };
        
        Console.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss} {prefix} {message}");
    }
    
    public void ShowExpiringNotification(int remainingDays)
    {
        string message = remainingDays > 0 
            ? $"授权将在{remainingDays}天后到期，请及时续期" 
            : "授权今日到期，请立即续期";
        
        ShowNotification(message, LicenseStatus.ExpiringSoon);
    }
    
    public void ShowExpiredNotification()
    {
        ShowNotification("授权已过期，请联系供应商续期", LicenseStatus.Expired);
    }
    
    public void ShowStatusChangeNotification(bool isValid)
    {
        string message = isValid ? "授权状态已恢复正常" : "授权状态异常，请检查";
        LicenseStatus status = isValid ? LicenseStatus.Valid : LicenseStatus.Expired;
        
        ShowNotification(message, status);
    }
}

/// <summary>
/// 日志文件通知实现
/// </summary>
public class LogFileNotifier : ILicenseNotifier
{
    private readonly string _logFilePath;
    
    public LogFileNotifier(string? logFilePath = null)
    {
        // 🔒 客户端集成库：不暴露具体路径，使用隐藏的日志文件
        _logFilePath = logFilePath ?? System.IO.Path.Combine(
            LicenseValidator.PATH, ".license_log.tmp");
    }
    
    public void ShowNotification(string message, LicenseStatus status)
    {
        try
        {
            string logEntry = $"{DateTime.Now:yyyy-MM-dd HH:mm:ss} [{status}] {message}";
            System.IO.File.AppendAllText(_logFilePath, logEntry + Environment.NewLine);
        }
        catch
        {
            // 忽略日志写入错误
        }
    }
    
    public void ShowExpiringNotification(int remainingDays)
    {
        string message = remainingDays > 0 
            ? $"授权将在{remainingDays}天后到期，请及时续期" 
            : "授权今日到期，请立即续期";
        
        ShowNotification(message, LicenseStatus.ExpiringSoon);
    }
    
    public void ShowExpiredNotification()
    {
        ShowNotification("授权已过期，请联系供应商续期", LicenseStatus.Expired);
    }
    
    public void ShowStatusChangeNotification(bool isValid)
    {
        string message = isValid ? "授权状态已恢复正常" : "授权状态异常，请检查";
        LicenseStatus status = isValid ? LicenseStatus.Valid : LicenseStatus.Expired;
        
        ShowNotification(message, status);
    }
}
